function X = mytrendarray(This,AltVec,IsDelog,Id,TVec)
% mytrendarray  [Not a public function] Create array with steady state paths for all variables.
%
% Backend IRIS function.
% No help provided.

% -IRIS Toolbox.
% -Copyright (c) 2007-2015 IRIS Solutions Team.

try
    AltVec;
catch
    AltVec = Inf;
end

try
    IsDelog;
catch
    IsDelog = true;
end

try
    Id; %#ok<VUNUS>
catch
    Id = 1 : length(This.name);
end

try
    TVec; %#ok<VUNUS>
catch
    TVec = This.Shift;
end

%--------------------------------------------------------------------------

realexp = @(x) real(exp(x));
nAlt = size(This.Assign,3);
nPer = length(TVec);
nId = length(Id);
if isequal(AltVec,Inf)
    AltVec = 1 : nAlt;
end
nAltVec = length(AltVec);

realId = real(Id);
imagId = imag(Id);
ixLog = This.IxLog(realId);
rep = ones(1,nPer);
sh = imagId(:);
sh = sh(:,rep);
sh = sh + TVec(ones(1,nId),:);

X = zeros(nId,nPer,nAltVec);
for i = 1 : nAltVec
    doTrendArray();
end


% Nested functions...


%**************************************************************************


    function doTrendArray()
        iAlt = AltVec(i);
        lvl = real(This.Assign(1,realId,min(iAlt,end)));
        grw = imag(This.Assign(1,realId,min(iAlt,end)));
        
        % Zero or no imag means zero growth also for log variables.
        grw(ixLog & grw == 0) = 1;
        
        ixGrw = (~ixLog & grw ~= 0) | (ixLog & grw ~= 1);
        
        % Level can be negative and log(level) complex for log variables; growth
        % must be positive for log variables.
        lvl(ixLog) = log(lvl(ixLog));
        grw(ixLog) = reallog(grw(ixLog));
        
        lvl = lvl.';
        grw = grw.';
        
        x = lvl(:,rep);
        if any(ixGrw)
            x(ixGrw,:) = x(ixGrw,:) ...
                + sh(ixGrw,:).*grw(ixGrw,rep);
        end
        
        % Delog only if requested.
        if IsDelog
            x(ixLog,:) = realexp(x(ixLog,:));
        end
        
        X(:,:,i) = x;
    end % doTrendArray()


end
